package com.xhsj.user.hr.process.processDefinition.web;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xhsj.user.hr.process.dto.processDefinition.AddBusinessDto;
import com.xhsj.user.hr.process.dto.processDefinition.ListBusinessDto;
import com.xhsj.user.hr.process.processDefinition.entity.TbGlobalBusiness;
import com.xhsj.user.hr.process.processDefinition.query.ListBusinessQuery;
import com.xhsj.user.hr.process.processDefinition.service.TbGlobalBusinessService;
import com.xhsj.user.utils.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

//import org.springframework.security.access.prepost.PreAuthorize;


/**
 * <p>
 * 流程设计-流程申请匹配表 前端控制器
 * </p>
 *
 * @author liuqinghua
 * @since Aug 28, 2020 4:37:27 PM
 */
@Slf4j
@Api(tags = "流程设计-流程申请匹配表")
@RestController
@RequestMapping("/process/tbGlobalBusiness")
@CrossOrigin(origins = "*", maxAge = 3600)
public class TbGlobalBusinessController {

	@Autowired
    public TbGlobalBusinessService service;


	/**
	 * 保存/修改
	 * @author liuqinghua
	 * @date Aug 28, 2020 4:37:27 PM
	 */
	@ApiOperation(value = "流程设计-流程申请匹配表,保存/修改", notes = "流程设计-流程申请匹配表,保存和修改方法")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
    // @PreAuthorize("hasAuthority('tb:global:business:edit')")
	public Message save(@RequestBody TbGlobalBusiness data){
    	try {
    		service.save(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
        	return Message.error(1,"失败");
		}

	}

	/**
	 * 通过id删除数据
	 * @author liuqinghua
	 * @date Aug 28, 2020 4:37:27 PM
	 */
	@ApiOperation(value = "流程设计-流程申请匹配表删除", notes = "通过id删除流程设计-流程申请匹配表信息。")
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
    // @PreAuthorize("hasAuthority('tb:global:business:delete')")
	public Message delete(@RequestParam(value="id") String id) {
		try {
			service.removeById(id);
        	return Message.success("成功");
		} catch (Exception e) {
			log.error("删除失败:{}",e);
            return Message.error(1,"失败");
		}

	}

	/**
	 * 批量删除
	 * @author liuqinghua
	 * @date Aug 28, 2020 4:37:27 PM
	 */
	@ApiOperation(value = "批量删除", notes = "批量删除。")
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	public Message batchDelete(@RequestBody List<String> list) {
		try {
			service.removeByIds(list);
    		return Message.success("成功");
		} catch (Exception e) {
    		log.error("删除失败:{}",e);
    		return Message.error(1,"失败");
		}
    }

    /**
    * 分页查询数据
    * @author liuqinghua
    * @date Aug 28, 2020 4:37:27 PM
    */
	@ApiOperation(value = "分页查询流程设计-流程申请匹配表list", notes = "带参数查询,{\"page\": 1,\"size\": 5,......}。支持模糊查询")
	@RequestMapping(value="/findList",method = RequestMethod.POST)
    // @PreAuthorize("hasAuthority('tb:global:business:view')")
	public Message findList(@RequestBody Map<String,Object> data) {
        try {
        	IPage<TbGlobalBusiness> selectPage = service.selectPage(data);
    		return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
        } catch (Exception e) {
			log.error("查询异常:{}",e);
    		return Message.error(1,"失败");
        }
	}

    /**
    * 查询所有数据
    * @author liuqinghua
    * @date Aug 28, 2020 4:37:27 PM
    */
	@ApiOperation(value = "查询流程设计-流程申请匹配表所有数据list", notes = "查询流程设计-流程申请匹配表所有数据list。")
	@RequestMapping(value="/findAll",method = RequestMethod.POST)
    // @PreAuthorize("hasAuthority('tb:global:business:view')")
	public Message findAll(){
        return Message.success("成功",service.selectAll());
	}

    /**
    * 通过id获取实体类
    * @author liuqinghua
    * @date Aug 28, 2020 4:37:27 PM
    */
	@ApiOperation(value = "通过id获取流程设计-流程申请匹配表实体类", notes = "通过id获取流程设计-流程申请匹配表实体类。")
	@RequestMapping(value="/getById",method = RequestMethod.GET)
    // @PreAuthorize("hasAuthority('tb:global:business:view')")
	public Message getById(@RequestParam(value="id") String id) {
    	return Message.success("成功",service.getById(id));
	}

    /**
    * 通过id查询详细信息
    * @author liuqinghua
    * @date Aug 28, 2020 4:37:27 PM
    */
	@ApiOperation(value = "通过id获取实体类详细信息", notes = "通过id获取实体类详细信息。")
	@RequestMapping(value="/detail",method = RequestMethod.GET)
    // @PreAuthorize("hasAuthority('tb:global:business:view')")
	public Message detail(@RequestParam(value="id") String id) {
    	return Message.success("成功",service.getById(id));
	}

	/**
	 * 流程列表展示
	 * @author liuqinghua
	 * @date Aug 28, 2020 4:36:28 PM
	 */
	@ApiOperation(value = "流程列表展示", notes = "申请类型表展示")
	@RequestMapping(value="/listBusiness",method = RequestMethod.POST)
	public Message listBusiness(@RequestBody @Valid ListBusinessDto data, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		IPage<ListBusinessQuery> listBusinessQueries = service.listBusiness(data);
		return Message.success("成功",listBusinessQueries.getRecords(),listBusinessQueries.getTotal(),listBusinessQueries.getPages());
	}

	/**
	 * @Description:  流程列表保存
	 * @Param:  com.xhsj.user.hr.process.dto.ProcessDefinition.AddBusinessDto
	 * @Author: liuqinghua
	 * @Date: 2020/8/31
	 */
	@ApiOperation(value = "流程列表保存", notes = "流程列表保存")
	@RequestMapping(value="/addBusiness",method = RequestMethod.POST)
	public Message addBusiness(@RequestBody AddBusinessDto data){
	    try {
            log.info("流程列表保存参数:{}",data);
           return service.addBusiness(data);
        }catch (Exception e){
            return Message.error(1,"失败");
        }
	}

	/**
	 * @Description:  流程列表修改
	 * @Param:  com.xhsj.user.hr.process.dto.ProcessDefinition.AddBusinessDto
	 * @Author: liuqinghua
	 * @Date: 2020/8/31
	 */
	@ApiOperation(value = "流程列表修改", notes = "流程列表修改")
	@RequestMapping(value="/updateBusiness",method = RequestMethod.POST)
	public Message updateBusiness(@RequestBody @Valid AddBusinessDto data, BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
        return service.updateBusiness(data);
	}

	/**
	 * @Description:  启用禁用状态修改
	 * @Author: liuqinghua
	 * @Date: 2020/8/31
	 */
	@ApiOperation(value = "启用禁用状态修改", notes = "启用禁用状态修改")
	@RequestMapping(value="/isEnableBusiness",method = RequestMethod.GET)
	public Message updateBusiness(@RequestParam(value = "id") long id,
								  @RequestParam(value = "isEnable") Integer isEnable) throws Exception {
		try {
			service.isEnableBusiness(id,isEnable);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("禁用异常,{}",e);
			return Message.error(1,e.getMessage());
		}

	}

    /**
     * @Description:  查看具体信息
     * @Author: liuqinghua
     * @Date: 2020/8/31
     */
    @ApiOperation(value = "查看具体信息", notes = "查看具体信息")
    @RequestMapping(value="/showBusiness",method = RequestMethod.GET)
    public Message showBusiness(@RequestParam(value = "id") long id) throws Exception {
        return Message.success("成功",service.showBusiness(id));
    }


}

